home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / bsddb / __init__.pyc (.txt) next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  11.2 KB  |  305 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Support for BerkeleyDB 3.3 through 4.4 with a simple interface.
  5.  
  6. For the full featured object oriented interface use the bsddb.db module
  7. instead.  It mirrors the Sleepycat BerkeleyDB C API.
  8. '''
  9.  
  10. try:
  11.     if __name__ == 'bsddb3':
  12.         import _pybsddb
  13.         _bsddb = _pybsddb
  14.         from bsddb3.dbutils import DeadlockWrap as _DeadlockWrap
  15.     else:
  16.         import _bsddb
  17.         from bsddb.dbutils import DeadlockWrap as _DeadlockWrap
  18. except ImportError:
  19.     import sys
  20.     del sys.modules[__name__]
  21.     raise 
  22.  
  23. db = _db = _bsddb
  24. __version__ = db.__version__
  25. error = db.DBError
  26. import sys
  27. import os
  28. if sys.version >= '2.3':
  29.     import UserDict
  30.     from weakref import ref
  31.     exec "\nclass _iter_mixin(UserDict.DictMixin):\n    def _make_iter_cursor(self):\n        cur = _DeadlockWrap(self.db.cursor)\n        key = id(cur)\n        self._cursor_refs[key] = ref(cur, self._gen_cref_cleaner(key))\n        return cur\n\n    def _gen_cref_cleaner(self, key):\n        # use generate the function for the weakref callback here\n        # to ensure that we do not hold a strict reference to cur\n        # in the callback.\n        return lambda ref: self._cursor_refs.pop(key, None)\n\n    def __iter__(self):\n        try:\n            cur = self._make_iter_cursor()\n\n            # FIXME-20031102-greg: race condition.  cursor could\n            # be closed by another thread before this call.\n\n            # since we're only returning keys, we call the cursor\n            # methods with flags=0, dlen=0, dofs=0\n            key = _DeadlockWrap(cur.first, 0,0,0)[0]\n            yield key\n\n            next = cur.next\n            while 1:\n                try:\n                    key = _DeadlockWrap(next, 0,0,0)[0]\n                    yield key\n                except _bsddb.DBCursorClosedError:\n                    cur = self._make_iter_cursor()\n                    # FIXME-20031101-greg: race condition.  cursor could\n                    # be closed by another thread before this call.\n                    _DeadlockWrap(cur.set, key,0,0,0)\n                    next = cur.next\n        except _bsddb.DBNotFoundError:\n            return\n        except _bsddb.DBCursorClosedError:\n            # the database was modified during iteration.  abort.\n            return\n\n    def iteritems(self):\n        if not self.db:\n            return\n        try:\n            cur = self._make_iter_cursor()\n\n            # FIXME-20031102-greg: race condition.  cursor could\n            # be closed by another thread before this call.\n\n            kv = _DeadlockWrap(cur.first)\n            key = kv[0]\n            yield kv\n\n            next = cur.next\n            while 1:\n                try:\n                    kv = _DeadlockWrap(next)\n                    key = kv[0]\n                    yield kv\n                except _bsddb.DBCursorClosedError:\n                    cur = self._make_iter_cursor()\n                    # FIXME-20031101-greg: race condition.  cursor could\n                    # be closed by another thread before this call.\n                    _DeadlockWrap(cur.set, key,0,0,0)\n                    next = cur.next\n        except _bsddb.DBNotFoundError:\n            return\n        except _bsddb.DBCursorClosedError:\n            # the database was modified during iteration.  abort.\n            return\n"
  32. else:
  33.     
  34.     class _iter_mixin:
  35.         pass
  36.  
  37.  
  38. class _DBWithCursor(_iter_mixin):
  39.     '''
  40.     A simple wrapper around DB that makes it look like the bsddbobject in
  41.     the old module.  It uses a cursor as needed to provide DB traversal.
  42.     '''
  43.     
  44.     def __init__(self, db):
  45.         self.db = db
  46.         self.db.set_get_returns_none(0)
  47.         self.dbc = None
  48.         self.saved_dbc_key = None
  49.         self._cursor_refs = { }
  50.  
  51.     
  52.     def __del__(self):
  53.         self.close()
  54.  
  55.     
  56.     def _checkCursor(self):
  57.         if self.dbc is None:
  58.             self.dbc = _DeadlockWrap(self.db.cursor)
  59.             if self.saved_dbc_key is not None:
  60.                 _DeadlockWrap(self.dbc.set, self.saved_dbc_key)
  61.                 self.saved_dbc_key = None
  62.             
  63.         
  64.  
  65.     
  66.     def _closeCursors(self, save = 1):
  67.         if self.dbc:
  68.             c = self.dbc
  69.             self.dbc = None
  70.             if save:
  71.                 
  72.                 try:
  73.                     self.saved_dbc_key = _DeadlockWrap(c.current, 0, 0, 0)[0]
  74.                 except db.DBError:
  75.                     pass
  76.                 except:
  77.                     None<EXCEPTION MATCH>db.DBError
  78.                 
  79.  
  80.             None<EXCEPTION MATCH>db.DBError
  81.             _DeadlockWrap(c.close)
  82.             del c
  83.         
  84.         for cref in self._cursor_refs.values():
  85.             c = cref()
  86.             if c is not None:
  87.                 _DeadlockWrap(c.close)
  88.                 continue
  89.         
  90.  
  91.     
  92.     def _checkOpen(self):
  93.         if self.db is None:
  94.             raise error, 'BSDDB object has already been closed'
  95.         
  96.  
  97.     
  98.     def isOpen(self):
  99.         return self.db is not None
  100.  
  101.     
  102.     def __len__(self):
  103.         self._checkOpen()
  104.         return (_DeadlockWrap,)((lambda : len(self.db)))
  105.  
  106.     
  107.     def __getitem__(self, key):
  108.         self._checkOpen()
  109.         return (None, _DeadlockWrap)((lambda : self.db[key]))
  110.  
  111.     
  112.     def __setitem__(self, key, value):
  113.         self._checkOpen()
  114.         self._closeCursors()
  115.         
  116.         def wrapF():
  117.             self.db[key] = value
  118.  
  119.         _DeadlockWrap(wrapF)
  120.  
  121.     
  122.     def __delitem__(self, key):
  123.         self._checkOpen()
  124.         self._closeCursors()
  125.         
  126.         def wrapF():
  127.             del self.db[key]
  128.  
  129.         _DeadlockWrap(wrapF)
  130.  
  131.     
  132.     def close(self):
  133.         self._closeCursors(save = 0)
  134.         if self.dbc is not None:
  135.             _DeadlockWrap(self.dbc.close)
  136.         
  137.         v = 0
  138.         if self.db is not None:
  139.             v = _DeadlockWrap(self.db.close)
  140.         
  141.         self.dbc = None
  142.         self.db = None
  143.         return v
  144.  
  145.     
  146.     def keys(self):
  147.         self._checkOpen()
  148.         return _DeadlockWrap(self.db.keys)
  149.  
  150.     
  151.     def has_key(self, key):
  152.         self._checkOpen()
  153.         return _DeadlockWrap(self.db.has_key, key)
  154.  
  155.     
  156.     def set_location(self, key):
  157.         self._checkOpen()
  158.         self._checkCursor()
  159.         return _DeadlockWrap(self.dbc.set_range, key)
  160.  
  161.     
  162.     def next(self):
  163.         self._checkOpen()
  164.         self._checkCursor()
  165.         rv = _DeadlockWrap(self.dbc.next)
  166.         return rv
  167.  
  168.     
  169.     def previous(self):
  170.         self._checkOpen()
  171.         self._checkCursor()
  172.         rv = _DeadlockWrap(self.dbc.prev)
  173.         return rv
  174.  
  175.     
  176.     def first(self):
  177.         self._checkOpen()
  178.         self._checkCursor()
  179.         rv = _DeadlockWrap(self.dbc.first)
  180.         return rv
  181.  
  182.     
  183.     def last(self):
  184.         self._checkOpen()
  185.         self._checkCursor()
  186.         rv = _DeadlockWrap(self.dbc.last)
  187.         return rv
  188.  
  189.     
  190.     def sync(self):
  191.         self._checkOpen()
  192.         return _DeadlockWrap(self.db.sync)
  193.  
  194.  
  195.  
  196. def hashopen(file, flag = 'c', mode = 438, pgsize = None, ffactor = None, nelem = None, cachesize = None, lorder = None, hflags = 0):
  197.     flags = _checkflag(flag, file)
  198.     e = _openDBEnv(cachesize)
  199.     d = db.DB(e)
  200.     d.set_flags(hflags)
  201.     if pgsize is not None:
  202.         d.set_pagesize(pgsize)
  203.     
  204.     if lorder is not None:
  205.         d.set_lorder(lorder)
  206.     
  207.     if ffactor is not None:
  208.         d.set_h_ffactor(ffactor)
  209.     
  210.     if nelem is not None:
  211.         d.set_h_nelem(nelem)
  212.     
  213.     d.open(file, db.DB_HASH, flags, mode)
  214.     return _DBWithCursor(d)
  215.  
  216.  
  217. def btopen(file, flag = 'c', mode = 438, btflags = 0, cachesize = None, maxkeypage = None, minkeypage = None, pgsize = None, lorder = None):
  218.     flags = _checkflag(flag, file)
  219.     e = _openDBEnv(cachesize)
  220.     d = db.DB(e)
  221.     if pgsize is not None:
  222.         d.set_pagesize(pgsize)
  223.     
  224.     if lorder is not None:
  225.         d.set_lorder(lorder)
  226.     
  227.     d.set_flags(btflags)
  228.     if minkeypage is not None:
  229.         d.set_bt_minkey(minkeypage)
  230.     
  231.     if maxkeypage is not None:
  232.         d.set_bt_maxkey(maxkeypage)
  233.     
  234.     d.open(file, db.DB_BTREE, flags, mode)
  235.     return _DBWithCursor(d)
  236.  
  237.  
  238. def rnopen(file, flag = 'c', mode = 438, rnflags = 0, cachesize = None, pgsize = None, lorder = None, rlen = None, delim = None, source = None, pad = None):
  239.     flags = _checkflag(flag, file)
  240.     e = _openDBEnv(cachesize)
  241.     d = db.DB(e)
  242.     if pgsize is not None:
  243.         d.set_pagesize(pgsize)
  244.     
  245.     if lorder is not None:
  246.         d.set_lorder(lorder)
  247.     
  248.     d.set_flags(rnflags)
  249.     if delim is not None:
  250.         d.set_re_delim(delim)
  251.     
  252.     if rlen is not None:
  253.         d.set_re_len(rlen)
  254.     
  255.     if source is not None:
  256.         d.set_re_source(source)
  257.     
  258.     if pad is not None:
  259.         d.set_re_pad(pad)
  260.     
  261.     d.open(file, db.DB_RECNO, flags, mode)
  262.     return _DBWithCursor(d)
  263.  
  264.  
  265. def _openDBEnv(cachesize):
  266.     e = db.DBEnv()
  267.     if cachesize is not None:
  268.         if cachesize >= 20480:
  269.             e.set_cachesize(0, cachesize)
  270.         else:
  271.             raise error, 'cachesize must be >= 20480'
  272.     
  273.     e.set_lk_detect(db.DB_LOCK_DEFAULT)
  274.     e.open('.', db.DB_PRIVATE | db.DB_CREATE | db.DB_THREAD | db.DB_INIT_LOCK | db.DB_INIT_MPOOL)
  275.     return e
  276.  
  277.  
  278. def _checkflag(flag, file):
  279.     if flag == 'r':
  280.         flags = db.DB_RDONLY
  281.     elif flag == 'rw':
  282.         flags = 0
  283.     elif flag == 'w':
  284.         flags = db.DB_CREATE
  285.     elif flag == 'c':
  286.         flags = db.DB_CREATE
  287.     elif flag == 'n':
  288.         flags = db.DB_CREATE
  289.         if file is not None and os.path.isfile(file):
  290.             os.unlink(file)
  291.         
  292.     else:
  293.         raise error, "flags should be one of 'r', 'w', 'c' or 'n'"
  294.     return flags | db.DB_THREAD
  295.  
  296.  
  297. try:
  298.     import thread
  299.     del thread
  300.     if db.version() < (3, 3, 0):
  301.         db.DB_THREAD = 0
  302. except ImportError:
  303.     db.DB_THREAD = 0
  304.  
  305.